import java.util.Scanner;
/**
 * @author Team 29
 *
 */
public class Database {

	public static Calendar clndr;
	
	/**
	 * @param s
	 * @return String to send back to client.
	 * processes the recieved request, sends the appropriate response back.
	 * Delegates the command to appropriate methods elsewhere.
	 */
	public static String respondToRequest(String s)
	{
		String r = null;
		Scanner scan = new Scanner(s);
		String s1 = scan.next();
		
		if(s1.equals("sndlogin"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			if(t1.contains("SUSPENDED"))
			r = "-1";
			else
			r = DatabaseUsers.loginAttempt(t1, t2);
		}
		if(s1.equals("newusr"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			String t3 = scan.next();
			String t4 = scan.next();
			String t5 = scan.next();
			String t6 = scan.next();
			r = DatabaseUsers.addUser(t1, t2, t3, t4, t5, t6);
		}
		if(s1.equals("edt_ptnt"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			String t3 = scan.next();
			String t4 = scan.next();
			String t5 = scan.next();
			String t6 = scan.next();
			String t7 = scan.next();
			r = DatabaseUsers.editUser(t1, t2, t3, t4, t5,t6, t7);
		}
		if(s1.equals("edt_privilege"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			r = DatabaseUsers.editPrivilege(t1, t2);
		}
		if(s1.equals("vw_bi"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.retrieveInfo(t1);
		}
		if(s1.equals("rqs_appt"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			String t3 = scan.next();
			String t4 = scan.next();
			String t5 = scan.next();
			String t6 = scan.next();
			String t7 = scan.next();
			r = clndr.requestAppointment(t1, t2, t3, t4, t5, t6, t7);
		}
		if(s1.equals("vw_med_hist"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.retrieveMedHist(t1);
		}
		if(s1.equals("edt_med_hist"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			r = DatabaseUsers.setMedHist(t1, t2);
		}
		if(s1.equals("vw_trt_rec"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.retrieveTrtRec(t1);
		}
		if(s1.equals("edt_trt_rec"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			r = DatabaseUsers.setTrtRec(t1, t2);
		}
		if(s1.equals("vw_doc_ord"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.retrieveDocOrd(t1);
		}
		if(s1.equals("edt_doc_ord"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			String t3 = scan.next();
			String t4 = scan.next();
			String t5 = scan.next();
			r = DatabaseUsers.setDocOrd(t1,t2,t3,t4,t5);
		}
		if(s1.equals("set_pw"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			r = DatabaseUsers.setPass(t1, t2);
		}
		if(s1.equals("rst_pw"))
		{
			String t1 = scan.next();
			r=DatabaseUsers.resetPass(t1);
		}
		if(s1.equals("vw_bill"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.retrieveBillingInfo(t1);
		}
		if(s1.equals("edt_bill"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			String t3 = scan.next();
			r = DatabaseUsers.editBillingInfo(t1, t2, t3);
		}
		if(s1.equals("vw_invoice"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.retrieveInvoice(t1);
		}
		if(s1.equals("edt_invoice"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			String t3 = scan.next();
			r = DatabaseUsers.editInvoice(t1,t2,t3);
		}
		if(s1.equals("vw_cal"))
		{
			r = clndr.toString();
		}
		if(s1.equals("vw_appt"))
		{
			String t1 = scan.next();
			for(Appointment a: clndr.getAppointmentRequests())
			{
				if(a.requester != null && a.requester.userID.equals(t1))
					{r = a.toString();
					return r;
					}
			}

			r = "-1";
		}
		if(s1.equals("vw_appt2"))
		{
			String t1 = scan.next();
			for(Appointment a: clndr.getApprovedAppointments())
			{
				if(a.requester != null && a.requester.userID.equals(t1))
					{r = a.toString();
					return r;
					}
			}
			
			r = "-1";
		}
		if(s1.equals("vw_pending"))
		{
			r = clndr.pendingString();
		}
		if(s1.equals("sspnd_acct"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.suspendAccount(t1);
		}
		if(s1.equals("proc_appt"))
		{
			String t1 = scan.next();
			String t2 = scan.next();
			r = clndr.processAppointment(t1, t2);
		}
		if(s1.equals("enbl_acct"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.unsuspendAccount(t1);
		}
		if(s1.equals("del_acct"))
		{
			String t1 = scan.next();
			r = DatabaseUsers.deleteAccount(t1);
		}
		if(s1.equals("vw_users"))
		{
			r = DatabaseUsers.sendUsers();
		}
		if(s1.equals("vw_stats"))
		{
			r = DatabaseUsers.getStats() + "\n" + clndr.getStats();
		}
		return r;
	}//end respond
}
